package com.ebelter.btcomlib.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import com.ebelter.btcomlib.R;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;


public class XieLine extends View {

    private static final String TAG = "XieLine";

    public XieLine(Context context) {
        this(context,null);
    }

    public XieLine(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public XieLine(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);

    }


    private String  xieLine1;

    private int xieLine1Color;
    private int xieLine1Width;



    private Paint paint;

    private int width;
    private int height;
    /**
     * 初始化
     */
    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.xieLineView, defStyleAttr, 0);
        int count = array.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = array.getIndex(i);
            if (attr==R.styleable.xieLineView_xieLine1){
                xieLine1 = array.getString(attr);//xy坐标轴颜色
            }else if (attr==R.styleable.xieLineView_xieLineColor1){//xy坐标轴宽度
                xieLine1Color = array.getColor(attr,0x000);
            }else if (attr==R.styleable.xieLineView_xieLine1Width){//xy坐标轴宽度
               float xieLine1Width2 = array.getDimension(attr,0);
               LogUtils.i(TAG,"xieLine1Width2="+xieLine1Width2);
                //xieLine1Width = dp2px((int) xieLine1Width2);
                xieLine1Width = (int) xieLine1Width2;
            }
        }
        array.recycle();


        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }






    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width=getMeasuredWidth();
        height=getMeasuredHeight();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawLine(canvas,xieLine1,xieLine1Color);


    }

    //还上下线
    private void drawLine(Canvas canvas,String lineInfo,int xieLine1Color) {
        if (TextUtils.isEmpty(lineInfo)){
            return;
        }
        String[] nums=lineInfo.split("#");
        if (nums.length!=2){
            return;
        }
        String firstPoint=nums[0];
        String secondPoint=nums[1];


        String[] firstStrs=firstPoint.split("-");
        String[] secondStrs=secondPoint.split("-");
        if (firstStrs.length!=2||secondStrs.length!=2){
            return;
        }

        float firstX = NumUtils.string2Float(firstStrs[0]);
        float firstY = NumUtils.string2Float(firstStrs[1]);

        float secondX = NumUtils.string2Float(secondStrs[0]);
        float secondY = NumUtils.string2Float(secondStrs[1]);

        paint.setColor(xieLine1Color);
        paint.setStrokeWidth(xieLine1Width);

        float startX = width*firstX;
        float startY = height*firstY;
        float stopX = width*secondX;
        float stopY = height*secondY;

        canvas.drawLine(startX,startY,stopX,stopY,paint);

    }
}
